package cn.joyfulmedia.common.log;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.rolling.RollingFileAppender;
import com.taobao.eagleeye.EagleEye;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 自定义日志格式file appender
 *
 * @author yujun
 * @create 2016-11-04 21:18
 */
public class CustomLogFileAppender extends RollingFileAppender {

    @Override
    protected void subAppend(Object event) {
        if (event instanceof LoggingEvent) {
            LoggingEvent loggingEvent = (LoggingEvent) event;
            loggingEvent.getMDCPropertyMap().put("ip", getIp());
            loggingEvent.getMDCPropertyMap().put("traceId", EagleEye.getTraceId());
            // if (loggingEvent.getLevel().levelInt == Level.INFO.levelInt) {
            //     loggingEvent.setMessage("[] []");
            // }
            // loggingEvent.getMDCPropertyMap().put("code", "[]");
            // loggingEvent.getMDCPropertyMap().put("msg", "[]");
        }
        super.subAppend(event);
    }

    private static String getIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "";
    }
}
